import { RoutersMap } from '@/router/routers';
import Vue from 'vue';
import { USER_RULE } from '@/store/mutation-types';
/**
 * 过滤账户是否拥有某一个权限，并将菜单从加载列表移除
 *
 * @param permission
 * @param route
 * @returns {boolean}
 */
function hasPermission(permission, route) {
  if (route.meta && route.meta.permission) {
    let flag = false;
    for (let i = 0, len = permission.length; i < len; i++) {
      flag = route.meta.permission.includes(permission[i]);
      if (flag) {
        return true;
      }
    }
    return false;
  }
  return true;
}

/**
 * 单账户多角色时，使用该方法可过滤角色不存在的菜单
 *
 * @param roles
 * @param route
 * @returns {*}
 */
// eslint-disable-next-line
function hasRole(roles, route) {
  if (route.meta && route.meta.roles) {
    return route.meta.roles.includes(roles.id);
  }
  return true;
}

function filterAsyncRouter(routerMap, roles) {
  const accessedRouters = routerMap.filter((route) => {
    if (hasPermission(roles.permissionList, route)) {
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children, roles);
      }
      return true;
    }
    return false;
  });
  return accessedRouters;
}
/**
 * @name: ViJia
 * @msg: 路由筛选
 * @param {*} routerMap
 * @param {*} user
 * @return {*} router
 */
function filterRouter(routerMap, Roles, MapRouterName) {
  const items = routerMap[0].children;
  const userRouter = Roles;
  const userMapRouterName = MapRouterName
  items.forEach((item, fIndex) => {
    if (item.children) {
      item.children = item.children.filter(item => userRouter.indexOf(item.path) !== -1 || item.children);
      item.children.forEach((childrenOne, oneIndex) => {
        if (childrenOne.children) {
          childrenOne.children = childrenOne.children.filter(item => userRouter.indexOf(item.path) !== -1);
        }
      });
    }
  });
  for (let i = items.length - 1; i >= 0; i--) {
    if (items[i].children) {
      if (items[i].children.length === 0) {
        items.splice(i, 1);
      } else {
        for (let j = items[i].children.length - 1; j >= 0; j--) {
          if (items[i].children[j].children) {
            if (items[i].children[j].children.length === 0) {
              items[i].children.splice(j, 1);
            }
          }
        }
        if (items[i].children.length === 0) {
          items.splice(i, 1);
        }
      }
    }
  }
  // items.forEach((item, index) => {
  //   // console.log(item);
  //   userMapRouterName.forEach((ims, idx) => {
  //     // console.log(ims);
  //     if (item.path == ims.route) {
  //       item.meta.title = ims.name
  //     }
  //     if (item.children && ims.child) {
  //       item.children.forEach((ChildItem, ChildIndex) => {
  //         ims.child.forEach((Childims, Childidx) => {
  //           if (ChildItem.path == Childims.route) {
  //             ChildItem.meta.title = Childims.name
  //           }
  //         })
  //       })
  //     }
  //   })
  // })
  ForRouterArr(items, userMapRouterName)
  routerMap[0].children = items;
  return routerMap;
}
/**
 * @name: ViJia
 * @msg: 路由name替换
 * @param {*} oneArr
 * @param {*} twoArr
 * @return {*} router
 */
function ForRouterArr(oneArr, twoArr) {
  oneArr.forEach((item, index) => {
    twoArr.forEach((items, indexs) => {
      // console.log(ims);
      if (item.path == items.route) {
        item.meta.title = items.name
      }
      if (item.children && items.child) {
        ForRouterArr(item.children, items.child)
      }
    })
  })
  return oneArr
}
const permission = {
  state: {
    routers: RoutersMap,
    addRouters: [],
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers;
      state.routers = routers;
    },
  },
  actions: {
    GenerateRoutes({ commit }) {
      // const Rule = Vue.ls.get(USER_RULE);
      // filterRouter(RoutersMap, Rule.RouteArr, Rule.RouterList);
      commit('SET_ROUTERS', RoutersMap);
    },
  },
};

export default permission;
